package com.huilian.iotbox.admin.wap.service.impl;

import com.huilian.iotbox.admin.wap.dao.SearchHistoryDao;
import com.huilian.iotbox.admin.wap.service.SearchHistoryService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.SearchHistoryDto;
import com.huilian.iotbox.data.enums.PermissionPlatformTypeEnum;
import com.huilian.iotbox.data.enums.SearchHistoryFunTypeEnum;
import com.huilian.iotbox.data.vo.SearchHistoryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author LinYinDong
 * @date 2024/10/25 下午4:23
 */
@Service
@Slf4j
public class SearchHistoryServiceImpl implements SearchHistoryService {
    @Autowired
    private SearchHistoryDao searchHistoryDao;
    @Override
    public ServerResponse addSearchHistory(SearchHistoryDto searchHistoryDto) {
        if(StringUtils.isEmpty(searchHistoryDto.getFunType())){
            return ServerResponse.createByErrorMessage("缺少操作分支");
        }
        if(!StringUtils.isEmpty(searchHistoryDto.getSearchTerm()) && searchHistoryDto.getSearchTerm().length() > 0) {
            searchHistoryDto.setPlatformType(PermissionPlatformTypeEnum.MERCHANT_APPLET.getCode());
            searchHistoryDto.setFunType(searchHistoryDto.getFunType());
            SearchHistoryVo searchHistoryVo = searchHistoryDao.getOneSearchHistory(searchHistoryDto);
            int result = -1;
            if(searchHistoryVo == null) {
                 result = searchHistoryDao.insert(searchHistoryDto);
            }else{
                searchHistoryDto.setId(searchHistoryVo.getId());
                 result = searchHistoryDao.update(searchHistoryDto);
            }
            if(result <= 0) {
                log.warn("操作关键字失败");
            }
        }else{
            log.warn("关键字为空，没有添加到数据库");
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse findSearchHistoryListByUserId(SearchHistoryDto searchHistoryDto) {
        if(StringUtils.isEmpty(searchHistoryDto.getFunType())){
            return ServerResponse.createByErrorMessage("缺少操作分支");
        }
        searchHistoryDto.setPlatformType(PermissionPlatformTypeEnum.MERCHANT_APPLET.getCode());
        searchHistoryDto.setFunType(searchHistoryDto.getFunType());
        final List<SearchHistoryVo> searchHistoryListByUserId = searchHistoryDao.findSearchHistoryListByUserId(searchHistoryDto);
        return ServerResponse.createBySuccess(searchHistoryListByUserId);
    }

    @Override
    public ServerResponse delAllSearchHistoryByUserId(SearchHistoryDto searchHistoryDto) {
        if(StringUtils.isEmpty(searchHistoryDto.getFunType())){
            return ServerResponse.createByErrorMessage("缺少操作分支");
        }
        searchHistoryDto.setPlatformType(PermissionPlatformTypeEnum.MERCHANT_APPLET.getCode());
        searchHistoryDto.setFunType(searchHistoryDto.getFunType());
        int result = searchHistoryDao.delete(searchHistoryDto);
        if(result <= 0) {
            ServerResponse.createByErrorMessage("删除历史记录失败");
        }
        return ServerResponse.createBySuccess(result);
    }
}
